<template>
    <form-container :title="formData.id ? '编辑车间信息' : '新增车间信息'" :width="550" :visible="visible" :destroy-on-close="true"
        @close="onClose">
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="编码" name="id">
                <a-input v-model:value="formData.id" allow-clear />
            </a-form-item>
            <a-form-item label="生产人员编码" name="employeeCode">
                <a-input v-model:value="formData.employeeCode" allow-clear />
            </a-form-item>
            <a-form-item label="生产人员名称" name="employeeName">
                <a-input v-model:value="formData.employeeName" allow-clear />
            </a-form-item>
            <a-form-item label="产线编码" name="lineCode">
                <a-select v-model:value="formData.lineCode" placeholder="请选择产线">
                    <a-select-option v-for="item in lines" :key="item.id" :value="item.id">{{ item.lineName
                    }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="性别" name="gender">
                <a-radio-group v-model:value="formData.gender">
                    <a-radio value="1">男</a-radio>
                    <a-radio value="2">女</a-radio>
                </a-radio-group>
            </a-form-item>
            <a-form-item label="工种类型" name="workType">
                <a-input v-model:value="formData.workType" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="班次组" name="shiftGroup">
                <a-select v-model:value="formData.shiftGroup" placeholder="请选择">
                    <a-select-option value="1">早</a-select-option>
                    <a-select-option value="2">中</a-select-option>
                    <a-select-option value="3">晚</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="状态" name="status">
                <a-select v-model:value="formData.status" placeholder="请选择">
                    <a-select-option value="1">启用</a-select-option>
                    <a-select-option value="2">禁用</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="入职日期" name="hireDate">
                <a-date-picker v-model:value="formData.hireDate" show-time type="date" placeholder="Pick a date"
                    style="width: 100%" />
            </a-form-item>
            <a-form-item label="备注信息" name="remark">
                <a-input v-model:value="formData.remark" placeholder="请输入名称" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="positionForm">
import { required } from '@/utils/formRules'
import { getCurrentInstance } from 'vue'
import { message } from 'ant-design-vue'
import tool from '@/utils/tool'
import lineApi from '@/api/mes/basicdata/lineApi'
import employeeApi from '@/api/production/employeeApi'

// 定义emit事件
const emit = defineEmits({ successful: null })
const { proxy } = getCurrentInstance()
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据，也就是默认给一些数据
const formData = ref({})
// 定义机构元素
const treeData = ref([])
const submitLoading = ref(false)
// 继续新增下一条
const addNext = ref(false)

// 打开抽屉
const onOpen = (record, orgId) => {
    visible.value = true
    addNext.value = false
    formData.value = {
        id: formData.value.id,
        employeeName: formData.value.employeeName,
        employeeCode: formData.value.employeeCode,
        lineCode: formData.value.lineCode,
        gender: formData.value.gender,
        workType: formData.value.workType,
        shiftGroup: formData.value.shiftGroup,
        status: formData.value.status,
        hireDate: formData.value.hireDate,
        remark: formData.value.remark,
    }
    if (record) {
        formData.value = Object.assign({}, record)
    }

}
// 关闭抽屉
const onClose = () => {
    visible.value = false
}
// 默认要校验的
const formRules = {
    sName: [required('请输入学生名称')],
    age: [required('请输入年龄')],
}

// 验证并提交数据
const onSubmit = () => {
    formRef.value.validate().then(() => {
        submitLoading.value = true
        employeeApi
            .addEmployee(formData.value, formData.value.id)
            .then(() => {
                if (addNext.value) {
                    formData.value = {
                        id: formData.value.id,
                        employeeName: formData.value.employeeName,
                        employeeCode: formData.value.employeeCode,
                        lineCode: formData.value.lineCode,
                        gender: formData.value.gender,
                        workType: formData.value.workType,
                        shiftGroup: formData.value.shiftGroup,
                        status: formData.value.status,
                        hireDate: formData.value.hireDate,
                        remark: formData.value.remark,
                    }
                } else {
                    visible.value = false
                }
                emit('successful')
            })
            .finally(() => {
                submitLoading.value = false
            })
    })
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen
})

//产线
const lines = ref([])
//获取产线
const queryLines = () => {
    lineApi.lineSelect({}).then(res => {
        //console.log(res)
        lines.value = res
    })
    //lineApi.lineSelect()
}


onMounted(() => {
    queryLines()
})
</script>
